package gazzera.swing.utility;

import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.filechooser.*;

public class Utils
{
	private static Map<String, Icon> icons = new HashMap<String, Icon>();
	
	public static Icon getSystemIcon(String extension)
	{
		Icon i = icons.get(extension);
        if(i==null)
        {
			try
			{
				//Create a temporary file with the specified extension
				File file = File.createTempFile("icon", "."+extension);
				    
				FileSystemView view = FileSystemView.getFileSystemView();    
				i = view.getSystemIcon(file);    

				//Delete the temporary file
				file.delete();
			}
			catch (Exception e)
			{
				i = new ImageIcon(ResourceLoader.get("page.png"));
			}
			icons.put(extension, i);
			
        }
		
		return i;
	}
	
	public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();
        Rectangle bounds = ge.getMaximumWindowBounds();
        int w = frame.getWidth(); // Math.max(bounds.width/2, Math.min(frame.getWidth(), bounds.width));
        int h = frame.getHeight(); //Math.max(bounds.height/2, Math.min(frame.getHeight(), bounds.height));
        int x = center.x - w/2, y = center.y - h/2;
        frame.setBounds(x, y, w, h);
        if (w == bounds.width && h == bounds.height)
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.validate();
    }

}
